package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.UserConnectAddDto;
import com.dog.basics.bean.dto.admin.common.UserConnectEditDto;
import com.dog.basics.bean.model.common.DogUserConnect;
import com.dog.basics.dao.common.DogUserConnectMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogUserConnectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户第三方信息 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-05-27
 */
@Service
public class DogUserConnectServiceImpl extends ServiceImpl<DogUserConnectMapper, DogUserConnect> implements IDogUserConnectService {
    /**
     * @Description: 增加用户第三方信息
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(UserConnectAddDto obj) {
        //通过userID查询数据
        List<DogUserConnect> list = this.list(Wrappers.<DogUserConnect>lambdaQuery().eq(DogUserConnect::getUserId, obj.getUserId()));
        if (CollUtil.isNotEmpty(list)) {
            DogUserConnect dogUserConnect = list.get(0);
            BeanUtil.copyProperties(obj, dogUserConnect);
            dogUserConnect.setUpdateTime(DateUtil.currentSeconds());
            dogUserConnect.setUpdateBy(obj.getUserId());
            return this.updateById(dogUserConnect);
        } else {
            DogUserConnect dogNews = new DogUserConnect();
            BeanUtil.copyProperties(obj, dogNews);
            dogNews.setCreateTime(DateUtil.currentSeconds());
            dogNews.setCreateBy(obj.getUserId());
            return this.save(dogNews);
        }

    }

    /**
     * @Description: 修改用户第三方信息
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(UserConnectEditDto obj) {
        DogUserConnect dogNews = this.getById(obj.getId());
        Validator.validateNotNull(dogNews, "无此数据");
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setUpdateTime(DateUtil.currentSeconds());
        dogNews.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        return this.updateById(dogNews);
    }
}
